package org.boun.swe.ARCEONE;

import org.boun.swe.ARCEONE.R;
import org.boun.swe.ARCEONE.Static.Constants;

import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

public class InitialActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.initial);
        goHomeIfConnected();
    }
    
	public void onClick_Retry(View v){
		goHomeIfConnected();
	}
	
	private void goHomeIfConnected(){
		if(!isNetworkAvailable() && !isGPSAvailable())
			Toast.makeText(getApplicationContext(), Constants.ERROR_NO_NETWORK_CONNECTION, Toast.LENGTH_LONG).show();
		else
			goHome();
	}
	
	private void goHome(){
		startActivity(new Intent(this, HomeActivity.class));
		finish();
	}
	
	private boolean isNetworkAvailable(){
		ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
		return activeNetworkInfo != null;
	}
	
	private boolean isGPSAvailable(){
		LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
	}
}
